package com.easymin.weixin.controller.res;

import com.easymin.weixin.common.api.DaijiaApi;
import com.easymin.weixin.common.api.PassengerApi;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.entity.passenger.Passenger;
import com.easymin.weixin.manager.WxApplicationManager;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api/passenger")
public class PassengerRes {
  
  private static final Logger logger = LoggerFactory.getLogger(PassengerRes.class);
  
  @Autowired
  private PassengerApi passengerApi;
  @Autowired
  private DaijiaApi daijiaApi;
  @Autowired
  private WxApplicationManager wxApplicationManager;
  
  /**
   * 查询单个乘客.
   */
  @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
  public @ResponseBody Map<String, Object> findPassenger(
      @RequestParam("passengerId") Long passengerId, 
      @RequestParam("appKey") String appKey) throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    appKey = StringUtils.trimToEmpty(appKey);


    Passenger passenger = passengerApi.findById(passengerId, appKey);

    try {
      map.put("code", 0);
      map.put("msg", "成功");
      map.put("data", passenger);
      return map;
    } catch (Exception ex) {
      logger.error(ex.getMessage());
      map.put("code", 500);
      map.put("msg", "查询地址失败");
      return map;
    }
  }

  /**
   * 获取优惠券.
   */
  
  @RequestMapping(value = {"getCoupons"}, method = RequestMethod.GET)
  public @ResponseBody Map<String, Object> getCoupons(
      @RequestParam("passengerId") Long passengerId, @RequestParam("appKey") String appKey) {
    Map<String, Object> map = new HashMap<String, Object>();
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
    try {
      map = daijiaApi.getCoupons(wxApplication.getAddress(), passengerId);
    } catch (Exception ex) {
      logger.error(ex.getMessage());
      map.put("code", 500);
      map.put("msg", "获取优惠券失败");
    }
    return map;
  }

  /**
   * 获取客户端分享二维码所需数据.
   */
  
  @RequestMapping(value = {"getQrCodeData"}, method = RequestMethod.GET)
  public @ResponseBody Map<String, Object> getQrCodeData(
      @RequestParam("passengerId") Long passengerId,
      @RequestParam("acKey") String acKey,
      @RequestParam("appKey") String appKey) {
    Map<String, Object> map = new HashMap<String, Object>();
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
    try {
      map = daijiaApi.getQrCodeData(wxApplication.getAddress(), passengerId, acKey);
    } catch (Exception ex) {
      logger.error(ex.getMessage());
      map.put("code", 500);
      map.put("msg", "获取客户端享二维码所需数据失败");
    }
    return map;
  }

  /**
   * 查询客户常用地址.
   */
  @RequestMapping(value = {"queryAddress"}, method = RequestMethod.GET)
  public @ResponseBody Map<String, Object> queryAddress(
      @RequestParam("passengerId") Long passengerId,
      @RequestParam("appKey") String appKey) {
    Map<String, Object> map = new HashMap<String, Object>();
    appKey = StringUtils.trimToEmpty(appKey);
    try {
      map = passengerApi.queryAddress(passengerId, appKey);
      return map;
    } catch (Exception ex) {
      logger.error(ex.getMessage());
      map.put("code", 500);
      map.put("msg", "地址查询失败");
      return map;
    }
  }

  /**
   * 保存客户常用地址.
   * @param type 类型:0家,1公司,2其他
   */
  @RequestMapping(value = {"saveAddress"}, method = RequestMethod.POST)
  public @ResponseBody Map<String, Object> saveAddress(
      @RequestParam("passengerId") Long passengerId, 
      @RequestParam("address") String address,
      @RequestParam("addressDetail") String addressDetail,
      @RequestParam("latitude") Double latitude,
      @RequestParam("longitude") Double longitude,
      @RequestParam("type") Integer type,
      @RequestParam("appKey") String appKey) {
    Map<String, Object> map = new HashMap<String, Object>();
    appKey = StringUtils.trimToEmpty(appKey);
    try {
      map =
          passengerApi.saveAddress(address, addressDetail, latitude, longitude, type, passengerId,
              appKey);
      return map;
    } catch (Exception ex) {
      logger.error(ex.getMessage());
      map.put("code", 500);
      map.put("msg", "地址查询失败");
      return map;
    }
  }

  /**
   * 移除客户常用地址.
   */
  @RequestMapping(value = {"removeAddress"}, method = RequestMethod.POST)
  public @ResponseBody Map<String, Object> removeAddress(
      @RequestParam("passengerId") Long passengerId,
      @RequestParam("addressId") Long addressId,
      @RequestParam("appKey") String appKey) {
    Map<String, Object> map = new HashMap<String, Object>();
    appKey = StringUtils.trimToEmpty(appKey);
    try {
      map = passengerApi.removeAddress(passengerId, addressId, appKey);
      return map;
    } catch (Exception ex) {
      logger.error(ex.getMessage());
      map.put("code", 500);
      map.put("msg", "移除常用地址失败");
      return map;
    }
  }
}
